package com.zhangtai.modules.controller.admin;


import java.util.*;

import com.alibaba.fastjson.JSONObject;
import com.zhangtai.modules.controller.aouthLogin.LoginController;
import com.zhangtai.modules.controller.aouthLogin.MemberLoginController;
import com.zhangtai.modules.dao.ReportBackInfoDao;
import com.zhangtai.modules.dto.FreqReportBackDto;
import com.zhangtai.modules.entity.employee.ReportBackInfoEntity;
import com.zhangtai.modules.service.employee.ReportBackInfoService;
import com.zhangtai.modules.vo.ReportBackVo;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.PageUtils;
import com.zhangtai.utils.R;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


/**
 *
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-12-11 16:09:07
 */
@RestController
@Api(tags="自助端销假申请")
@RequestMapping("/reportBack")
@Slf4j
public class ReportBackInfoController {
    @Autowired
    private ReportBackInfoService reportBackInfoService;
    @Autowired
    private MemberLoginController memberLoginController;
    @Autowired
    private ReportBackInfoDao reportBackInfoDao;
    @Autowired
    private LoginController loginController;

    /**
     * 列表
     */
    @GetMapping("/listReport")
    @ApiOperation(value = "分页查询-销假申请")
    public R listReportback(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
            , HttpServletRequest request) throws Exception {
        //获取当前用户角色下公司Id
        JSONObject data = memberLoginController.getUserInfo(request).getData();
        if (ObjectUtils.isEmpty(data)){
            return R.error("请先登录");
        }
        String usercode = data.getString("usercode"); //只能查询自己的信息


        PageResult result=  reportBackInfoService.queryPage(null,null,usercode,null
                ,null,pageNum,pageSize);
        return R.ok(result);

    }


    /**
     * 列表
     */
    @GetMapping("/listReportSearch")
    @ApiOperation(value = "HR端分页查询-销假查询")
    public R listReportbackSearch(@RequestParam(value = "orgName", required = false) String orgName,
                                  @RequestParam(value = "unitName", required = false) String unitName,
                                  @RequestParam(value = "jobName", required = false) String jobName,
                                  @RequestParam(value = "userName", required = false) String userName,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
            , HttpServletRequest request) throws Exception {
        R uR = loginController.getUserInfo(request);
        //获取当前用户角色下公司Id
        R checkR = loginController.checkOrgPermission(request,null,false);
        if(!R.isOk.equals(checkR.getStatus())){
            return checkR;
        }
        List<Long> orgIds = (List<Long>) checkR.getData();
        PageResult result=  reportBackInfoService.queryPageHr( orgName,unitName,jobName,userName,null,orgIds,null
                ,null,pageNum,pageSize);
        return R.ok(result);

    }


    /**
     * 信息
     */
    @GetMapping("/getReportBackById")
    @ApiOperation(value = "根据id查询-销假申请")
    public R getReprtById(@RequestParam Long id){
        ReportBackInfoEntity reportBackInfo = reportBackInfoService.getByIdE(id);

        return R.ok(reportBackInfo);
    }

    @GetMapping("/getReportBackFrequency")
    @ApiOperation(value="根据员工工号查询待销假的列表")
    public R getReportFrequency(@RequestParam(value = "oid") String oid,
                               HttpServletRequest request) throws Exception {
        JSONObject data = memberLoginController.getUserInfo(request).getData();
        if (ObjectUtils.isEmpty(data)){
            return R.error("请先登录");
        }
        //暂时自定义
        //后面去东软取
        JSONObject object=new JSONObject();
        FreqReportBackDto freqReportBackDto1=new FreqReportBackDto();
        freqReportBackDto1.setDayNum("1");
        freqReportBackDto1.setType(0);
        freqReportBackDto1.setBeginGmtDate("2020-11-11 09:12");
        freqReportBackDto1.setEndGmtDate("2020-11-12 16:00");

        FreqReportBackDto freqReportBackDto2=new FreqReportBackDto();
        freqReportBackDto2.setDayNum("2");
        freqReportBackDto2.setType(1);
        freqReportBackDto2.setBeginGmtDate("2022-11-12 09:12");
        freqReportBackDto2.setEndGmtDate("2020-11-13 16:00");


        FreqReportBackDto freqReportBackDto3=new FreqReportBackDto();
        freqReportBackDto3.setDayNum("3");
        freqReportBackDto3.setType(0);
        freqReportBackDto3.setBeginGmtDate("2022-11-12 09:12");
        freqReportBackDto3.setEndGmtDate("2020-11-13 18:00");


        List<FreqReportBackDto> frequencyList=new ArrayList<FreqReportBackDto>();
        frequencyList.add(freqReportBackDto1);
        frequencyList.add(freqReportBackDto2);
        frequencyList.add(freqReportBackDto3);
        object.put("frequencyList",frequencyList);
        return R.ok(object);
    }
    /**
     * 保存
     */
    @PostMapping("/saveReportBack")
    @ApiOperation(value="保存或修改销假信息")
    @Transactional
    public R saveAndUpdateReportBackInfo(@RequestBody @Valid ReportBackInfoEntity reportBackInfoEntity, HttpServletRequest request) throws Exception {
        JSONObject data = memberLoginController.getUserInfo(request).getData();
        Long oid = data.getLong("id"); //只能查询自己的信息
//        Long oid =  new Long(50989147);
        if (ObjectUtils.isEmpty(oid)){
            return R.error("请先登录");
        }

        ReportBackInfoEntity vo=reportBackInfoService.getByIdE(reportBackInfoEntity.getId());
        String usercode = data.getString("usercode"); //只能查询自己的信息
//        String usercode="ZT028817";
//        Long orgId=new Long(328339);
//        Long jobId=new Long(14511268);
//        Long unitId=new Long(14492744);
        Long orgId=data.getLong("orgId");
        Long jobId=data.getLong("jobId");
        Long unitId=data.getLong("depId");


        if (vo ==null){
            reportBackInfoEntity.setCreatetime(new Date());
            reportBackInfoEntity.setIsDelete(0);
            reportBackInfoEntity.setJobId(jobId);
            reportBackInfoEntity.setOrgId(orgId);
            reportBackInfoEntity.setUnitId(unitId);
            reportBackInfoEntity.setCode(usercode);
            reportBackInfoEntity.setOid(oid);
            reportBackInfoService.saveE(reportBackInfoEntity);
        }else{
            reportBackInfoService.updateByIdE(reportBackInfoEntity);
        }
        return R.ok();
    }



    /**
     * 删除
     */
    @DeleteMapping("/deleteReportBack")
    @ApiOperation("删除调班信息")
    @Transactional
    public R deleteReportBack(@RequestParam(value = "id",required = true) String id){
        reportBackInfoService.removeOneById(new Long(id));
        return R.ok();
    }



}